package com.zimmur.service.impl.base;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.zimmur.service.RedisCacheUtil;
import com.zimmur.service.base.UserBrowserHistoryService;
import com.zimmur.solr.model.Goods;
import com.zimmur.solr.service.ISolrGoodsService;


@Scope("prototype")
@Service
public class UserBrowserHistoryServiceImpl implements UserBrowserHistoryService {
	
	@Resource
	private RedisCacheUtil<String> redisUtil;
	@Resource
	private ISolrGoodsService solrGoodsService;
	
	private final String Redis_UserBrowerKey="popolook-browsergoodslog";

	@Override
	public boolean recordGoodsHistory(String userId, String goodsId) {
		List<String> cacheList = redisUtil.getCacheList(Redis_UserBrowerKey+"-"+userId, 0);
		if(!cacheList.isEmpty()){
			Set<String> hashSet = new  HashSet<String>(cacheList);
			if(hashSet.contains(goodsId)){
				return true;
			}
		}
		
		redisUtil.addCacheList(Redis_UserBrowerKey+"-"+userId, goodsId);

		return true;
	}

	@Override
	public List<Goods> queryHistoryByUserId(String userId,int size) {
		List<Goods> goodsList = null;
		if(size==0){
			size = -1;
		}
		List<String> cacheList = redisUtil.getCacheList(Redis_UserBrowerKey+"-"+userId, size);
		if(!cacheList.isEmpty()){
			goodsList = solrGoodsService.queryGoodsById(cacheList);
		}
		return goodsList;
	}

}
